package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * com.sky.controller.user
 * AddressBookController
 *
 * @since 2024/11/3
 */
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端地址簿接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "新增地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook)
    {
        log.info("新增地址",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询登录用户所有地址
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询登录用户所有地址")
    public Result<List<AddressBook>> list()
    {
        log.info("查询登录用户所有地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }

    /**
     * 设置默认地址
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "设置默认地址")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook)
    {
        log.info("设置默认地址");
        AddressBook result = addressBookService.setDefault(addressBook);
        return Result.success(result);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation(value ="查询默认地址")
    public Result<AddressBook> getDefault()
    {
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        if(list.isEmpty()) return Result.error("没有查询到默认地址");
        return Result.success(list.get(0));
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址");
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址");
        AddressBook byId = addressBookService.getById(id);
        System.out.println("byId = " + byId);
        return Result.success(byId);
    }

    @DeleteMapping
    @ApiOperation(value = "根据id删除地址")
    public Result delete(@RequestParam Long id){
        log.info("根据id删除地址");
        addressBookService.deleteById(id);
        return Result.success();
    }
}
